const express = require('express');
const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const socketController = require('./socketController');

// max # of sockets to keep open
const socketLimit = 1000;


// takes in Node Server instance and returns Express Router
module.exports = function createNileServer(server, socketLimit) {
  // Pass server instance to use socket controller
  const controller = new socketController(server, socketLimit);

  // create nile.js mini-app through express Router
  const nileServer = express.Router();

  // parse request body
  nileServer.use(bodyParser.json());

  // endpoint for receiving magnet URI from Broadcaster
  nileServer.post('/magnet', (req, res, next) => {
    controller.emitNewMagnet(req.body.magnetURI);
    res.sendStatus(200);
  });

  // room page
  nileServer.get('/room/:roomID', function (req, res) {
    var roomID = req.params.roomID;
    
    //res.sendFile(`${__dirname}/client/room.html?roomId=${roomID}`)
    //return;

    // 渲染页面数据(见views/room.hbs)
    res.render('room', { roomID: roomID });
  });
  
  return nileServer;
}